package cn.hpu.mobile_bank.controller;


import cn.hpu.mobile_bank.domain.Recipient;
import cn.hpu.mobile_bank.dto.req.AddOrUpdateRecipientReq;
import cn.hpu.mobile_bank.dto.resp.ApiResult;
import cn.hpu.mobile_bank.service.RecipientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/recipient")
@Tag(name = "收款人管理")
public class RecipientController {

    @Autowired
    private RecipientService recipientService;

    @GetMapping("/list")
    @Operation(summary = "获取收款人列表")
    public ApiResult<List<Recipient>> listByTel() {
        return recipientService.listByTel();
    }

    @PostMapping("/add")
    @Operation(summary = "添加收款人")
    public ApiResult<String> addRecipient(@RequestBody @Valid AddOrUpdateRecipientReq addOrUpdateRecipientReq) {
        return recipientService.addRecipient(addOrUpdateRecipientReq);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据id获取收款人信息")
    public ApiResult<Recipient> getRecipient(@PathVariable("id") Integer id) {
        return ApiResult.success(recipientService.getById(id));
    }

    @PostMapping("/update")
    @Operation(summary = "更新收款人信息")
    public ApiResult<String> updateRecipient(@RequestBody @Valid AddOrUpdateRecipientReq addOrUpdateRecipientReq) {
        return recipientService.updateRecipient(addOrUpdateRecipientReq);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除收款人")
    public ApiResult<Boolean> deleteRecipient(@PathVariable("id") Integer id) {
        return ApiResult.success(recipientService.removeById(id));
    }


}
